/*
 * packet.h
 *
 *  Created on: 2013. 10. 16.
 *      Author: jphong
 */

#ifndef __PACKET_H__
#define __PACKET_H__

// Packet Structure
#define MAX_DATA	  	1400
#define NOOP				0
#define DATA				1
#define ACK				2
#define END			   3
typedef struct {
	unsigned char type;		// DATA, ACK, or END
	unsigned char seq;		// sequence number
	unsigned short len;		// including header size. network byte-order
	unsigned short checksum;	// host byte-order (not necessarily network-byte order
	unsigned short unused;	// should be cleared by 0
} Header;
typedef struct {		// for DATA packet
	Header header;
	char data[MAX_DATA];
} Packet;
typedef struct {		// for ACK, END
	Header header;
} CPacket;

// Event Types
typedef enum {
	TIMEOUT,
	RDT_SEND,
	RDT_RCV
} Event;

int udt_send(int sockfd, const Packet *pkt, const struct sockaddr *peer, socklen_t peerlen);
void start_timer(unsigned int msec );
void stop_timer();
Packet *make_pkt(unsigned char type, unsigned char seq, Packet *pkt, int datalen);
void print_pkt(const char *str, const Packet *pkt);
int isCorrupt(const Packet *pkt);
int isType(const Packet *pkt,unsigned char pktType, unsigned char seq);
Event getEvent(int fd, int sockfd);
uint16_t in_cksum(uint16_t *addr, int len);

#endif  /* __PACKET_H__ */
